from data_define import OrderInfo
import json


class FileReader:
    def read(self) -> list[OrderInfo]:
        pass


class CSVFileReader(FileReader):
    """读取csv数据"""
    def __init__(self, path):
        self.path = path

    def read(self) -> list[OrderInfo]:
        result = []
        f = open(self.path, 'r', encoding='UTF-8')
        lines = f.readlines()
        for line in lines:
            line = line.strip()
            data_list = line.split(',')
            result.append(OrderInfo(data_list[0], data_list[1], int(data_list[2]), data_list[3]))

        return result


class JSONFileReader(FileReader):
    """读取JSON数据"""
    def __init__(self, path):
        self.path = path

    def read(self) -> list[OrderInfo]:
        result = []
        f = open(self.path, 'r', encoding='UTF-8')
        lines = f.readlines()
        for line in lines:
            line = line.strip()
            line_dict = json.loads(line)
            order_info = OrderInfo(line_dict['date'], line_dict['order_id'], int(line_dict['money']),
                                   line_dict['province'])
            result.append(order_info)

        return result


if __name__ == '__main__':
    csv_file_reader = CSVFileReader('E:/PythonBasicKnowledge/10 面向对象/数据分析案例/2011年1月销售数据.txt')
    order_info_list1 = csv_file_reader.read()
    for item in order_info_list1:
        print(F'❀{item}')

    json_file_reader = JSONFileReader('E:/PythonBasicKnowledge/10 面向对象/数据分析案例/2011年2月销售数据JSON.txt')
    order_info_list2 = json_file_reader.read()
    for item in order_info_list2:
        print(F'🍀{item}')
